<template>
	<view class="bind-wechat-authorization">
		<g-navbar title="更换绑定微信账号" />
		<view class="main">
			<uni-forms class="form-main" :modelValue="formData" :rules="rules" ref="resetForm">
				<view class="form-container">
					<uni-forms-item name="phone">
						<view class="form-item">
							<view class="label">
								手机号码
							</view>
							<view class="input-wrapper">
								<uni-easyinput type="number" placeholder="请输入手机号码" :inputBorder="false"
									v-model="formData.phone" />
							</view>
						</view>
					</uni-forms-item>
					<uni-forms-item name="sms_code">
						<view class="form-item">
							<view class="label">
								验证码
							</view>
							<view class="input-wrapper">
								<uni-easyinput type="number" placeholder="请输入验证码" :inputBorder="false" :maxlength="6"
									v-model="formData.sms_code" />
							</view>
							<g-vcode @start="vcodeStart" :disabled="vcodeDisabled" :seconds="60" id="wxauthorzation"
								ref="vcode" @clickvcode="clickvcode" />
						</view>
					</uni-forms-item>
				</view>
			</uni-forms>
			<view class="btn-wrapper">
				<g-button btn_text="确定" :height="104" @click="onClickBtn" />
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { IFormData } from './types';
	import { reactive, Ref, ref, computed } from "vue";
	import { GET_STORE_USERINFO } from '@/store/gettersType';
	import { useStore } from 'vuex';
	import {
		mobile
	} from '@/utils/test';
	import { onLoad, onReady } from '@dcloudio/uni-app';
	import REST_URL from '@/http/http.restUrl';
	import global from '@/global';
	import { VERSION_TYPE } from '@/enum/version';
	import { ROUTE_TYPE } from '@/enum/router';
	
	

	const store = useStore();

	const resetForm : Ref<any> = ref(null);

	const vcode : Ref<any> = ref(null);


	const formData : IFormData = reactive({
		phone: '',
		sms_code: '',
		code: '',
		customer_id: store.getters[GET_STORE_USERINFO].id,
	})

	const vcodeDisabled = computed(() => {
		return !(mobile(formData.phone));
	})

	const rules = {
		phone: {
			rules: [{
				required: true,
				errorMessage: '请输入手机号码',
			},
			{
				validateFunction: (rule, value, data, callback) => {
					if (mobile(value)) {
						return true
					} else {
						callback('请输入正确的手机号')
					}
				}
			},
			]
		},
		sms_code: {
			rules: [{
				required: true,
				errorMessage: '请输入验证码'
			},
			{
				minLength: 6,
				errorMessage: '请输入6位验证码'
			}
			]
		},
	}

	onReady(() => {
		resetForm.value.setRules(rules);
	})


	onLoad((e) => {
		const searchParams = new URL(window.location.href).searchParams;
		const params = Object.fromEntries(searchParams);
		console.log(params);
		formData.code = params.code;
		if (e) {
			formData.phone = e.phone ? e.phone : store.getters[GET_STORE_USERINFO].phone;
			formData.sms_code = e.sms_code;
		}
		if (formData.code) {
			confirmResetWechatAuth();
		}
		global.resetWebUrl('/pages/bind_wechat_authorization/bindWechatAuthorization');
	})

	const onClickBtn = () => {
		resetForm.value.validate().then((form : loginFormData) => {
			console.log(form);
			// window.location.href = `http://localhost:9999/#/pages/bind_wechat_authorization/bindWechatAuthorization?phone=${formData.phone}&sms_code=${formData.sms_code}`
			 global.authorization(`/pages/bind_wechat_authorization/bindWechatAuthorization?phone=${formData.phone}&sms_code=${formData.sms_code}`);
		})
	}

	const vcodeStart = () => {

	}

	const clickvcode = async () => {
		await uni.http.POST(REST_URL.sendCode, {
			phone: formData.phone
		})
		vcode.value.changeStatus();
		uni.prompt.$showToast('已发送验证码');
	}

	const confirmResetWechatAuth = () => {
		uni.showModal({
			title: '确认重设微信授权',
			content: '原微信授权绑定账号将无法用此微信号进行微信授权登录',
			success: async(res) => {
				if (res.confirm) {
					await reset();
				}
			}
		})
	}

	const reset = async () => {
		await uni.http.POST(REST_URL.resetWXAuth, formData);

		if (store.state.version == VERSION_TYPE.EXP_VERSION) {
			uni.router.route({
				url: '/pages/home/home',
				type: ROUTE_TYPE.SWITCHTAB,
				success: () => {
					uni.prompt.$showToast('修改微信授权账号成功');
					// global.resetWebUrl('/pages/home/home');
				}
			});
		} else {
			uni.router.route({
				url: '/pages/statistics/statistics',
				type: ROUTE_TYPE.SWITCHTAB,
				success: () => {
					uni.prompt.$showToast('修改微信授权账号成功');
					// global.resetWebUrl('/pages/statistics/statistics');
				}
			});
		}
	}
</script>

<style scoped lang="scss">
	@import "./bindWechatAuthorization.scss";
</style>